from django.http import HttpResponse
from django.shortcuts import render, redirect
import requests
# Create your views here.
def get_data():
    params = {
            "cname": "湘潭",
            "pid": "",
            "pageIndex": 1,
            "pageSize": "10"
    }
    url = "https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
    headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
            "Cookie":"__utmz=62931061.1732767443.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=62931061.806578909.1732767443.1732767443.1733278636.2; route-cell=ksa; ASP.NET_SessionId=gjprwjdthpektzjoaioxq4oi; VOLCALBCORS=5065c71485bb7361a81e5558302cee16|1734097661|1734097537; VOLCALB=5065c71485bb7361a81e5558302cee16|1734097661|1734097537",
        }
    response = requests.post(url=url, headers=headers, params=params)
    data_list = response.json()
    return data_list

def kendeji(request):
    # 1. 定义一些新闻(字典或者列表) 或 去数据库 网络请求去联通新闻
    # 向地址：https://news.sina.com.cn/
    # django中的request(参数)与第三方模块requests不一样]
    return render(request, "app_01/kendeji.html", {"news_list": get_data()})


def something(request):
    # request是一个对象，封装了用户发送过来的所有请求数据
    # 1.获取请求方式 GET/POST
    print(request.method)

    # 2.在URL上传递值 /something/?n1=888
    print(request.GET)

    # 3.在请求体中提交数据
    print(request.POST)

    # 4.HttpResponse("返回内容")，内容字符串内容返回给请求者。
    # return HttpResponse("返回内容")
    # return HttpResponse("返回内容")

    # 5.【响应】 读取HTML的内容 + 渲染(替换) ->字符串，返回给用户浏览器
    # return render(request, "app_01/something.html", {"title": "cnm"})

    # 6.【响应】让浏览器重定向到其他页面
    return redirect("https://www.deepseek.com/")

def login(request):
    if request.method == "GET":
        return render(request, "app_01/login.html")
    else:
        # 如果是POST请求，获取用户提交的数据
        # print(request.POST)
        username = request.POST.get("user")
        password = request.POST.get("pwd")
        if username == "root" and password == "2743YZL,":
            return redirect("https://www.deepseek.com/")
        else:
            return render(request, "app_01/login.html", {"error_msg": "用户名或密码登录失败"})


from .models import UserInfo  # 修改导入名称
def info_list(request):
    # 1. 获取数据库中所有的信息
    # [对象， 对象， 对象]
    data_list = UserInfo.objects.all()

    # 2.渲染, 返回给用户
    return render(request, "app_01/info_list.html", {"data_list": data_list})

def info_add(request):
    if request.method == "GET":
        return render(request, "app_01/info_add.html")

    # 获取用户数据
    user = request.POST.get("user")
    pwd = request.POST.get("pwd")
    age = request.POST.get("age")

    # 添加到数据库
    UserInfo.objects.create(name=user, password=pwd, age=age)

    # 自动跳转
    # return redirect("http://127.0.0.1:8000/app_01/info/list/")
    # 跳转自己的网站
    # return redirect("/app_01/info/list/")


def info_delete(request):
    nid = request.GET.get("nid")
    UserInfo.objects.filter(id=nid).delete()
    return redirect("/app_01/info/list/")
